<?php

/**
 * @link https://www.humhub.org/
 * @copyright Copyright (c) 2015 HumHub GmbH & Co. KG
 * @license https://www.humhub.com/licences
 */

#namespace humhub\modules\directory\components;
namespace humhub\modules\api\cccstream;

use Yii;
use humhub\modules\user\models\User;

/**
 * UserPostsStreamAction
 *
 * @package humhub.modules_core.directory
 * @author luke
 * @since 0.11
 */
//class UserPostsStreamAction extends \humhub\modules\content\components\actions\Stream
class UserPostsStreamAction extends Stream
{

    public function init()
    {
        parent::init();

        $this->activeQuery->andWhere(['content.visibility' => \humhub\modules\content\models\Content::VISIBILITY_PUBLIC]);

        $p_type = \Yii::$app->request->get('p_type');
        $p1type = \Yii::$app->request->get('p1type');
        if( $p_type==1 && in_array($p1type, array('1','2','3'))){
            $this->activeQuery->leftJoin('wall', 'wall_entry.wall_id=wall.id');
            $this->activeQuery->leftJoin('space', 'wall.object_model=:spaceModel AND wall.object_id=space.id', ['spaceModel' => \humhub\modules\space\models\Space::className()]);
            $times = time();
            //筛选任务: 1=报名中， 2=进行中，3=已完成
            if( $p1type==1 ) $this->activeQuery->andWhere("space.s_t1>$times");
            if( $p1type==2 ) $this->activeQuery->andWhere("space.s_t1<$times AND space.s_t2>$times");
            if( $p1type==3 ) $this->activeQuery->andWhere("space.s_t2<$times");
        }

        //$wallIdsQuery = (new \yii\db\Query())
        //        ->select('wall_id')
        //        ->from('user uw');
        //if (Yii::$app->user->isGuest) {
        //    $wallIdsQuery->andWhere('visibility=' . User::VISIBILITY_ALL);
        //}
        //$wallIdsSql = Yii::$app->db->getQueryBuilder()->build($wallIdsQuery)[0];
        //$this->activeQuery->andWhere('wall_entry.wall_id IN (' . $wallIdsSql . ')');
    }

}
